package edu.cmu.ece.ds.droidchat.controller;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.model.ChatSessionInfo;

public class ChatSessionListBaseAdapter extends BaseAdapter {

    // the chat sessions
    private ArrayList<ChatSessionInfo> chatSessionArray;

    private LayoutInflater mInflater;

    public ChatSessionListBaseAdapter(Context context,
            ArrayList<ChatSessionInfo> chatSessionArray) {
        this.chatSessionArray = chatSessionArray;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return chatSessionArray.size();
    }

    @Override
    public Object getItem(int position) {
        return chatSessionArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.chat_sessions_list_item,
                    null);
            holder = new ViewHolder();
            holder.chatSessionNameTextView = (TextView) convertView
                    .findViewById(R.id.chat_session_name);
            holder.infoLineTextView = (TextView) convertView
                    .findViewById(R.id.chat_session_info);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ChatSessionInfo csInfo = chatSessionArray.get(position);
        holder.chatSessionNameTextView.setText(csInfo.getChatSessionName());
        if (csInfo.isPending()) {
            holder.infoLineTextView.setTextColor(Color.RED);
            holder.infoLineTextView.setText(R.string.pending);
            holder.chatSessionNameTextView.setTextColor(Color.GRAY);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView chatSessionNameTextView;
        TextView infoLineTextView;
    }

}
